package md.lanjing.com.module_wallet.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.event.BusFactory;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import butterknife.BindView;
import butterknife.OnClick;
;

import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.event.EventImpl;

import md.lanjing.com.module_wallet.R;
import md.lanjing.com.module_wallet.R2;
import md.lanjing.com.module_wallet.bean.RealNameResponce;
import md.lanjing.com.module_wallet.bean.TransferAccountResponce;
import md.lanjing.com.module_wallet.contact.TransferContact;
import retrofit2.Response;

/**
 * 转帐
 */
public class TransferAccountActivity extends MvpActivity<TransferContact.TransferPresent> implements TransferContact.ITransferView {
    @BindView(R2.id.et_account)
    EditText etAccount;
    @BindView(R2.id.et_money)
    EditText etMoney;
    @BindView(R2.id.tv_type)
    TextView tvType;
    @BindView(R2.id.btn_confirm_deal_pwd)
    Button btnConfirmDealPwd;
    @BindView(R2.id.tv_name)
    TextView tvName;

   // int type = -1;//0-cawd，1-usdt
    private boolean otherIsReal=false;//对方是否实名
    @Override
    public void initData(Bundle savedInstanceState) {
        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!ObjectUtils.isEmpty(s)&& RegexUtils.isMobileExact(s.toString())) {
                    mPresent.getRealNameByPhone(context, s.toString());
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_transfer_account;
    }

    @OnClick({R2.id.btn_confirm_deal_pwd, R2.id.tv_type})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_confirm_deal_pwd) {
            String account = etAccount.getText().toString();
            String money = etMoney.getText().toString();
              /*  if (type == -1) {
                    ToastUtils.showLongToast(context, "请选择币种");
                    return;
                }*/
            if (ObjectUtils.isEmpty(account)) {
                ToastUtils.showLongToast(context, "帐户不能为空");
                return;
            }
            if (ObjectUtils.isEmpty(money)) {
                ToastUtils.showLongToast(context, "金额不能为空");
                return;
            }
            if (!otherIsReal) {
                ToastUtils.showLongToast(context, "对方未实名或帐户不存在");
                return;
            }
            showDealDialog(account, Double.valueOf(money));

        } else if (i == R.id.tv_type) {//       showTypeDialog();

        }
    }

/*
    AlertDialog typeDialog = null;
    private void showTypeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_type_select)
                .fromBottom(true)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(context, 80))
                .setOnClickListener(R.id.tv_one, v -> {
                    type = 1;
                    tvType.setText("USDT");
                    typeDialog.dismiss();
                }).setOnClickListener(R.id.tv_two, v -> {
                    type = 0;
                    tvType.setText("CAWD");
                    typeDialog.dismiss();
                });
        typeDialog = builder.create();
        typeDialog.show();
    }*/


    AlertDialog dealDialog = null;

    private void showDealDialog(String account, double money) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss()).setOnClickListener(R.id.tv_sure, v -> {
                    PswText pswText = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = pswText.getPsw();
                    dealDialog.dismiss();
                    mPresent.transfer(context, 1, money, dealPwd, account);//只能转usdt
                });
        dealDialog = builder.create();
        dealDialog.show();
    }

    @Override
    public void getTransferData(Response<TransferAccountResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "转帐成功");
            BusFactory.getBus().post(new EventImpl.TransferEvent());
            finish();
        } else if (response.body().getCode() == 201) {
            ToastUtils.showLongToast(context, "系统错误");
        } else if (response.body().getCode() == 202) {
            ToastUtils.showLongToast(context, "代币不足");
        } else if (response.body().getCode() == 203) {
            ToastUtils.showLongToast(context, "目标账户不存在");
        } else if (response.body().getCode() == 204) {
            ToastUtils.showLongToast(context, "交易密码错误");
        } else if (response.body().getCode() == 205) {
            ToastUtils.showLongToast(context, "你没有实名认证");
        } else if (response.body().getCode() == 206) {
            ToastUtils.showLongToast(context, "对方没有实名认证");
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getRealNameByPhoneData(Response<RealNameResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            otherIsReal=true;
            tvName.setTextColor(Color.parseColor("#ffb4b4b4"));
            tvName.setText(response.body().getData());
        } else if (response.body().getCode() == 202) {
            tvName.setTextColor(Color.RED);
            tvName.setText("对方没有实名认证");
        } else if (response.body().getCode() == 203) {
            tvName.setTextColor(Color.RED);
            tvName.setText("账户不存在");
        } else {
            tvName.setTextColor(Color.RED);
            tvName.setText("系统错误");
        }
    }

    @Override
    protected TransferContact.TransferPresent createPresent() {
        return new TransferContact.TransferPresent();
    }
}
